package emailtask

import (
	"cvevulner/cve-timed-task/db_models"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
	"io/ioutil"
)

// Sending Query all recipients
func Sending(filePath string) {
	ormModel := orm.NewOrm()
	result, _, gErr := db_models.GetCveEmailListByEmailType("1", ormModel)
	if gErr != nil {
		logs.Error("db_models.GetCveEmailListByEmailType error:", gErr)
		return
	}

	fList := make([]string, 0, 10)
	fileList, err := ioutil.ReadDir(filePath)
	if err != nil {
		logs.Error("ReadDir error:", filePath, err.Error())
	}

	for _, file := range fileList {
		if file.IsDir() {
			continue
		}
		fileName := file.Name()
		childFilePath := filePath + fileName
		fList = append(fList, childFilePath)
	}

	subject := "This time cve creates an issue record"
	content := "The email attachment may contain two attached excel documents, which are the issue data that has been successfully created and the issue data that has not been created."

	for _, i := range result {
		err = SendEmail([]string{i.EmailName}, []string{}, subject, content, fList)
		if err != nil {
			logs.Error("SendEmail error:", err)
		}
	}

}
